<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.GregorianCalendar,java.util.Calendar,java.util.regex.*,java.util.*" %>
<%@page import="java.rmi.registry.LocateRegistry"%>
<%@page import="java.rmi.registry.Registry"%>
<%@page import="java.util.ArrayList"%>
<%@page import="rmi.*" %>
<%@page import="data.*" %>
<jsp:useBean id="manager" scope="session" class="manager.Manager" />
<%
String numero_securite_sociale="";

int page_lecture=1 ;
int nbOrdonnance=0;
int nb_page=0;
int page_min=0 ;
int page_max=0 ;
int nb_element_par_page=20 ;
int premier_ordonnance ;
int nbPatient;
Iterator it;
Iterator itPatient;
ArrayList<Ordonnance> list=new ArrayList<Ordonnance>() ;
ArrayList<Patient> listPatient=new ArrayList<Patient>() ;

if(request.getParameter("submit") != null){ 
	numero_securite_sociale = request.getParameter("numero_securite_sociale");
	if(numero_securite_sociale == null) numero_securite_sociale = "";
}

nbOrdonnance=manager.getServeurRmi().nbEnregistrements(Ordonnance.class) ;
list=manager.getServeurRmi().listerOrdonnances(numero_securite_sociale,"",0,nbOrdonnance) ;
nbOrdonnance=list.size() ;
	
if(request.getParameter("page") != null){ 
	page_lecture=Integer.parseInt(request.getParameter("page")) ;
}else{
	page_lecture=1 ;
}

nb_page=(nbOrdonnance/nb_element_par_page)+1 ;

if(page_lecture<=0) page_lecture=1 ; else if(page_lecture>nb_page) page_lecture=nb_page;


premier_ordonnance=(page_lecture-1)*nb_element_par_page ;

list=manager.getServeurRmi().listerOrdonnances(numero_securite_sociale,"",premier_ordonnance,nb_element_par_page) ;
it=list.iterator() ;

nbPatient=manager.getServeurRmi().nbEnregistrements(Patient.class);
listPatient=manager.getServeurRmi().listerPatients("","","",0,nbPatient) ;
itPatient=listPatient.iterator() ;
%>



<div id="contenu_style1_haut">
</div>
<div id="contenu_style1_centre">
	<form action="lister_ordonnance.jsp" method="post">
	<table>
		<tr>
			<td style="text-align:left;font-weight:bold;text-decoration:underline;font-size:14px;">Recherche</td>
			<td></td>
			<td></td>
		</tr>
		<tr>
			<td></td>
			<td style="text-align:center;font-weight:bold;">Patient :</td>
			<td>
				<select name="numero_securite_sociale">
					<option value=""></option>
	<%
					while(itPatient.hasNext()) {
						Patient p=(Patient) itPatient.next() ;
						if(numero_securite_sociale.trim().equals(p.getNumero_securite_sociale_patient())) {
	%>
							<option value="<%= p.getNumero_securite_sociale_patient() %>" selected="selected"><%= p.getNumero_securite_sociale_patient() %> | <%= p.getNom_patient() %> <%= p.getPrenom_patient() %></option>
	<%				
						}else{
	%>
							<option value="<%= p.getNumero_securite_sociale_patient() %>"><%= p.getNumero_securite_sociale_patient() %> | <%= p.getNom_patient() %> <%= p.getPrenom_patient() %></option>
	<%						
						}
					}
	%>				
				</select>
			</td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
		</tr>
		<tr> 
			<td>&nbsp;</td>
			<td>&nbsp;</td> 
			<td  style="text-align:right;"><input type="submit" name="submit" value="Rechercher" /></td> 
		</tr> 
	</table>
	</form>
</div>
<div id="contenu_style1_bas">
</div>

<%
if(list.size()==0) {
%>
	<div id="contenu_style2_haut">
	</div>
	<div id="contenu_style2_centre">
		<table>
			<tr>
				<td style="text-align:center;font-weight:bold;font-size:14px;">Aucune ordonnance enregistrée pour ce patient</td>
			</tr>
		</table>
	</div>
	<div id="contenu_style2_bas">
	</div>	
<%	
}else{
%>

<table>
	<tr>
<%
		if(page_lecture>1) {
%>
			<td style="width:35px;"><a href="lister_ordonnance.jsp?page=<%= page_lecture-1%>"><img src="../images/design/precedent.png" style="width:30px;"/></a></td>
		
<%			
		}else{
%>
			<td style="width:35px;">&nbsp;</td>
<%			
		}
%>	
		<td style="text-align:center;">
<%
			if(nb_page<=10) {
				page_min=1;
				page_max=nb_page;
			}else if(nb_page>10) {
				if(page_lecture<5) {
					page_min=1;
					page_max=10;
				}else if(page_lecture>(nb_page-5)){
					page_min=nb_page-9;
					page_max=nb_page;
				}else{
					page_min=page_lecture-4;
					page_max=page_lecture+5;
				}	
			}
					
			for(int i=page_min;i<=page_max;i++) {
				if(i==page_lecture) {
%>
					<a href="lister_ordonnance.jsp?page=<%= i %>" style="color:black;font-weight:bold;font-size:14px;"><%= i %></a>
<% 							
				}else{
%>
					<a href="lister_ordonnance.jsp?page=<%= i %>" style="color:black;"><%= i %></a>
<%		
				}
			}

%>		
		</td>
<%
		if(page_lecture<nb_page) {
%>
			<td style="width:35px;"><a href="lister_ordonnance.jsp?page=<%= page_lecture+1%>"><img src="../images/design/suivant.png" style="width:30px;"/></a></td>
		
<%			
		}else{
%>
			<td style="width:35px;">&nbsp;</td>
<%			
		}
%>
	</tr>
</table>
<div id="contenu_style2_haut">
</div>
<div id="contenu_style2_centre">
	<table>
		<tr>
			<th>Date</th>
			<th>Patient</th>
			<th>Médecin</th>
			<th>Visualiser</th>
		</tr>
<%
		while(it.hasNext()) {
			Ordonnance ordonnance=(Ordonnance) it.next() ;
			String jour=ordonnance.getDate_ordonnance().substring(8,10);
			String mois=ordonnance.getDate_ordonnance().substring(5,7);
			String annee=ordonnance.getDate_ordonnance().substring(0,4);
			Patient p=manager.getServeurRmi().getPatient(ordonnance.getNumero_securite_sociale_patient());
			Medecin medecin=manager.getServeurRmi().getMedecin(ordonnance.getId_medecin());
%>
			<tr>
				<td style="text-align:center;font-weight:bold;"><%= jour %>/<%= mois %>/<%= annee %></td>
				<td style="text-align:center;"><%= p.getNom_patient() %> <%= p.getPrenom_patient() %></td>
				<td style="text-align:center;"><%= medecin.getNom_medecin() %> <%= medecin.getPrenom_medecin() %></td>
				<td style="text-align:center;"><a href="/cabinet_medical/PDFOrdonnance?id_ordonnance=<%= ordonnance.getId_ordonnance() %>" target="_blank"><img src="../images/design/pdf.PNG" style="width:40px;" /></a></td>
			</tr>
<%		
		}
%>
	</table>
</div>
<div id="contenu_style2_bas">
</div>
<%
}
%>



